package com.jwm.power.util;

import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Chunk;
import com.itextpdf.text.Font;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.draw.LineSeparator;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;
import java.io.OutputStream;

public class PdfUtil {
    /**
     * 生成文件
     * @param baos
     * @param response
     * @param fileName
     * @throws Exception
     */
    public static void writeFile(ByteArrayOutputStream baos, HttpServletResponse response, String fileName) throws Exception {
        OutputStream out = response.getOutputStream();
        try {
            response.setHeader("Content-disposition", "attachment;filename=" + new String(fileName.getBytes("GBK"), "ISO-8859-1"));
            response.setContentType("application/pdf;charset=UTF-8");
            response.setCharacterEncoding("UTF-8");
            response.setContentLength(baos.size());
            baos.writeTo(out);
        } catch (Exception e) {
            throw e;
        } finally {
            if (out != null) {
                out.flush();
                out.close();
            }
            if (baos != null) {
                baos.close();
            }
        }
    }

    /**
     * 初始化头部
     * @param headers
     * @param table
     * @param font
     * @param cellHeight
     */
    public static void initHeaders(String[] headers, PdfPTable table, Font font, Integer cellHeight) {
        for (int i = 0; i < headers.length; i++) {
            PdfPCell cell = new PdfPCell(new Paragraph(headers[i], font));
            cell.setHorizontalAlignment(1);
            cell.setVerticalAlignment(5);
            cell.setFixedHeight(cellHeight);
            table.addCell(cell);
        }
    }

    /**
     * 设置并返回字体
     * @param request
     * @return
     * @throws Exception
     */
    public static BaseFont setBaseFont(HttpServletRequest request){
        BaseFont baseFont = null;
        try {
            // 原字体
            baseFont = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return baseFont;
    }

    public static void AddCell(PdfPTable table, Font headFont, int Border, int Colspan, int Rowspan, int rowHeight, String title, int ALIGN_CENTER , int ALIGN_TOP)
    {
        Paragraph paragraphRemark = new Paragraph();
        if(title.length() == 0){
            Chunk tChunk2 = new Chunk(new LineSeparator());
//            tChunk2.setBackground(BaseColor.CYAN, 1f, 0.5f, 1f, 1.5f); // 设置背景色
            tChunk2.setTextRise(6); // 上浮
            tChunk2.setUnderline(0.2f, -2f); // 下划线
//            paragraphRemark.add(Chunk.NEWLINE);
            paragraphRemark.add(tChunk2);
        }else {
            paragraphRemark = new Paragraph(title, headFont);
        }
        PdfPCell cell = new PdfPCell(paragraphRemark);//创建新列
        cell.setBorder(Border) ;
        cell.setColspan(Colspan);
        if (Rowspan > 0)
        {
            cell.setRowspan(Rowspan);
        }
        cell.setMinimumHeight(rowHeight);
        cell.setHorizontalAlignment(ALIGN_CENTER);//水平位置
        cell.setVerticalAlignment(ALIGN_TOP);
        table.addCell(cell);
    }
}
